package alo;

import java.util.Arrays;

/**
 * 冒泡排序
 */
public class L_BubbleSort {
    public static void main(String[] args) {
        double[] arr = {2.0, 2.1, 9, 5.02, 6, 3.04, 8, 4, 0.3, 5, 3.22, 6.2};
        System.out.println(Arrays.toString(arr));
        System.out.println("--------------sort flow------------");
        double[] sorted = bubbleSort(arr, true);
    }

    public static double[] bubbleSort(double[] arr, boolean debug) {
        int temp = arr.length;
        boolean hasChange = true;
        for (int k = temp; k >= 0; k--) {
            hasChange = false;
            for (int i = 0; i < temp - 1; i++) {
                double pre = arr[i];
                double next = arr[i + 1];
                //交换数据
                if (pre > next) {
                    arr[i + 1] = pre;
                    arr[i] = next;
                    hasChange = true;
                }
            }
            temp--;
            if (!hasChange) break;
            if (debug) System.out.println("sort: " + Arrays.toString(arr));
        }
        return arr;
    }
}
